import { Instrusion } from "@/Battle/Intrusion.js";
import CombatEventBus from "@/Battle/CombatEventBus.js";
import { BattleEventType } from "@/Battle/Event/BattleEvent.js";
export class ActionTransition extends Instrusion{
    constructor(){super();}

    onload(){
        CombatEventBus.on(
            BattleEventType.Action.Start.Complete,
            this.onActionStart
        );
        CombatEventBus.on(
            BattleEventType.Action.Update,
            this.onActionUpdate
        );
    }
    onActionStart=(event)=>{
        let source=event.payload.source;
        let item=event.payload.item;
        source.useItem(item);
        event.payload.action.applyGlobal();
    }
    onActionUpdate=(event)=>{
        let payload=event.payload;
        let source=payload.source;
        let target=payload.target;
        let item=payload.item;

        let animationId=item.animationId;
        this.showAnimation(source,target,animationId);

        event.interrupt();
    }
    onunload(){
        CombatEventBus.off(
            BattleEventType.Action.Start.Complete,
            this.onActionStart
        );
        CombatEventBus.off(
            BattleEventType.Action.Update,
            this.onActionUpdate
        );
    }

    showAnimation(subject, target, animationId) {
        if (animationId < 0) {
            this.showAttackAnimation(subject, target);
        } else {
            this.showNormalAnimation(target, animationId);
        }
    }

    showAttackAnimation(subject, target) {
        if (subject.isActor()) {
            this.showActorAttackAnimation(subject, target);
        } else {
            this.showEnemyAttackAnimation(subject, target);
        }
    }

    showActorAttackAnimation(subject, target) {
        this.showNormalAnimation(target, subject.attackAnimationId1(), false);
        this.showNormalAnimation(target, subject.attackAnimationId2(), true);
    }

    showEnemyAttackAnimation(subject, targets) {
        //SoundManager.playEnemyAttack();
    }

    showNormalAnimation(target, animationId, mirror) {
        let animation = $dataAnimations[animationId];
        if (animation) {
            target.startAnimation(animationId, mirror, 8);
        }
    }

}